package internal

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/name5566/leaf/gate"
	"server/g"
	"server/libs/db"
	"server/msg/pb"
	"strconv"
)

const (
	SQUARE_POS   = "square:pos"
	SQUARE_PLACE = "square:place"
	Radius_Near  = 500
)

type RotatePosInfo struct {
	Position_X   float64 `json:"Position_X"`
	Position_Y   float64 `json:"Position_Y"`
	Position_Z   float64 `json:"Position_Z"`
	RotateInfo_X float64 `json:"RotateInfo_X"`
	RotateInfo_Y float64 `json:"RotateInfo_Y"`
	RotateInfo_Z float64 `json:"RotateInfo_Z"`
}

func UpdateMetaSquare(args []interface{}) {
	agent := args[1].(gate.Agent)
	if agent.UserData() == nil {
		agent.WriteMsg(&pb.GameErrMsgResponse{
			ErrorCode: 1001,
			ErrorMsg:  "Please Login in",
		})
		return
	}
	message := args[0].(*pb.GameSquarePosRequest)
	// 添加经纬度位置信息
	// 使用带超时的上下文
	u := agent.UserData().(g.UserData)
	idStr := fmt.Sprintf("%d", u.Id)
	ctx := context.Background()
	//_, err := db.Redis().GeoAdd(ctx, SQUARE_POS, &redis.GeoLocation{
	//	Name:      idStr,
	//	Longitude: message.Pos_Y,
	//	Latitude:  message.Pos_X,
	//}).Result()
	//if err != nil {
	//	agent.WriteMsg(&pb.GameErrMsgResponse{
	//		ErrorCode: 1003,
	//		ErrorMsg:  err.Error(),
	//	})
	//	return
	//}
	// 将结构体实例转换为JSON字符串
	posInfo1JSON, err := json.Marshal(RotatePosInfo{
		Position_X:   message.Pos_X,
		Position_Y:   message.Pos_Y,
		Position_Z:   message.Pos_Z,
		RotateInfo_X: message.Rot_X,
		RotateInfo_Y: message.Rot_Y,
		RotateInfo_Z: message.Rot_Z,
	})
	if err != nil {
		agent.WriteMsg(&pb.GameErrMsgResponse{
			ErrorCode: 1003,
			ErrorMsg:  err.Error(),
		})
		return
	}

	// 将JSON字符串存储到Redis的Hash结构中
	err = db.Redis().HSet(ctx, SQUARE_PLACE, idStr, string(posInfo1JSON)).Err()
	if err != nil {
		agent.WriteMsg(&pb.GameErrMsgResponse{
			ErrorCode: 1003,
			ErrorMsg:  err.Error(),
		})
		return
	}
	agent.WriteMsg(&pb.GameSquarePosResponse{})
}

func GetNearUserSquare(args []interface{}) {
	agent := args[1].(gate.Agent)
	if agent.UserData() == nil {
		agent.WriteMsg(&pb.GameErrMsgResponse{
			ErrorCode: 1001,
			ErrorMsg:  "Please Login in",
		})
		return
	}
	//u := agent.UserData().(g.UserData)
	ctx := context.Background()
	results, err := db.Redis().HGetAll(ctx, SQUARE_PLACE).Result()
	if err != nil {
		fmt.Println("Error getting values:", err)
		return
	}
	var PosInfo pb.GameNearInfoResponse
	for key, jsonString := range results {
		var value RotatePosInfo
		err := json.Unmarshal([]byte(jsonString), &value)
		if err != nil {
			fmt.Println("Error unmarshaling JSON:", err)
			continue
		}
		int64Value, err := strconv.ParseInt(key, 10, 64)
		if err != nil {
			fmt.Println("String to int64 conversion error:", err)
		}
		PosInfo.Data = append(PosInfo.Data, &pb.NearUserInfo{
			UserId: int64Value,
			Pos_X:  value.Position_X,
			Pos_Y:  value.Position_Y,
			Pos_Z:  value.Position_Z,
			Rot_X:  value.RotateInfo_X,
			Rot_Y:  value.RotateInfo_Y,
			Rot_Z:  value.RotateInfo_Z,
		})
		fmt.Printf("Key: %s, Decoded Value: %+v\n", key, value)
	}
	agent.WriteMsg(&PosInfo)
}
